package com.webservice.config;


import com.webservice.service.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.xml.ws.Endpoint;

/***
 * 服务发布类
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private UserService userService;

    //spring boot2.0.6之后的版本与xcf集成，不需要定义如下方法：
 /*   @Bean
    public ServletRegistrationBean dispatcherServlet(){
        //发布服务名称
        return new ServletRegistrationBean(new CXFServlet(),"/service/*");
    }*/

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }

    /*@Bean
    public UserService userService(){
        return new UserServiceImpl();
    }*/

    @Bean
    public Endpoint endpoint(){
        //绑定要发布的服务
        EndpointImpl endpoint = new EndpointImpl(springBus(),userService);

        //显示要发布的名称
        endpoint.publish("/user");
        return endpoint;
    }

    /*@Bean
    public ServletRegistrationBean restServlet() {
        //注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //项目包名
        applicationContext.scan("com.webservice");
        DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/*");
        return registrationBean;
    }*/

}
